package com.angel.infrastructure.db.repository;

import com.angel.domain.angelmission.aggregates.AngelMission;
import com.angel.domain.angelmission.repositories.AngelMissionRepository;
import com.angel.infrastructure.db.mapper.MissionInfoMapper;
import com.angel.infrastructure.db.mapper.UserMissionMapper;
import com.angel.infrastructure.db.po.MissionInfoPO;
import com.angel.infrastructure.db.po.UserMissionPO;
import org.springframework.stereotype.Repository;

/*
 * 更新关注微信公众号： 为了offer
 * @author parkin
 * @description
 * @date 2021/11/16
 */
@Repository
public class DefaultAngelMissionRepository implements AngelMissionRepository {

    private final MissionInfoMapper missionInfoMapper;
    private final UserMissionMapper userMissionMapper;

    public DefaultAngelMissionRepository(MissionInfoMapper missionInfoMapper,
                                         UserMissionMapper userMissionMapper) {
        this.missionInfoMapper = missionInfoMapper;
        this.userMissionMapper = userMissionMapper;
    }


    @Override
    public AngelMission save(AngelMission angelMission) {
        MissionInfoPO missionInfoPO = new MissionInfoPO(angelMission.getAngelMissionId().getId(),
                angelMission.getAngelMissionInfo().getMissionName(),
                angelMission.getAngelMissionInfo().getMissionType(),
                angelMission.getAngelMissionInfo().getAngelHelpId().getId(),
                angelMission.getCreateInfo().getCreateTime());

        missionInfoMapper.insert(missionInfoPO);

        UserMissionPO userMissionPO = new UserMissionPO(
                angelMission.getAngelUserId().getId(),
                angelMission.getAngelMissionId().getId(),
                angelMission.getMissionState().get(),
                angelMission.getAngelMissionInfo().getCompleteTime()
        );
        userMissionMapper.insert(userMissionPO);

        return angelMission;
    }
}
